package com.neusoft.nep.NEPM.controller;

import com.neusoft.nep.entity.nepgResult;
import com.neusoft.tools.AlertUtils;
import com.neusoft.tools.FXMLUtils;
import com.neusoft.tools.FileUtils;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import java.io.IOException;
import java.util.*;

public class OtherStatisViewController {
    @FXML
    TreeItem  to_detailOfAqiView;
    @FXML
    TreeView treeview;
    @FXML
    TreeItem to_publicAqiDataListView;
    @FXML
    private Button btn_out;
    @FXML
    private TreeItem To_AqiTrendTotalStatisView;
    @FXML
    private TreeItem To_AqiSeperatedStatisView;
    @FXML
    private TreeItem To_OtherStatisView;
    @FXML
    private TreeItem To_ProvinceDataTotalStatisView;
    @FXML
    TreeItem To_confirmDataView;
    @FXML
    TreeItem To_publicAqiDataList;
    @FXML
    private Label aqiAll_num;
    @FXML
    private Label aqiGood_num;
    @FXML
    private Label provinceRange;
    @FXML
    private Label cityRange;
    Set<String> city=new HashSet<>();
    Set<String> city_judge=new HashSet<>();
    @FXML
    public void initialize(){
        treeview.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue == To_AqiSeperatedStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiSeperatedStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_AqiTrendTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/AqiTrendTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_OtherStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/OtherStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            } else if (newValue == To_ProvinceDataTotalStatisView) {
                try {
                    FXMLUtils.switchToShow(btn_out,"NEPM/view/ProvinceDataTotalStatisView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }

            }
            else if (newValue == to_publicAqiDataListView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataListView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
            else if (newValue == To_confirmDataView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/confirmDataView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }else if (newValue == To_publicAqiDataList) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/publicAqiDataList.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }else if (newValue == to_detailOfAqiView) {
                try {
                    FXMLUtils.switchToShow(btn_out, "NEPM/view/detailOfAqiView.fxml");
                } catch (IOException e) {
                    e.printStackTrace();
                    AlertUtils.showDialog("错误", "页面跳转失败: " + e.getMessage());
                }
            }
        });
        List<nepgResult> RecordData=new ArrayList();
        RecordData= FileUtils.readListObject("NEPGresult.txt");
        int aqiAll=RecordData.size();
        int aqiGood=0,bigCity=0;
        Double proRange,cityrange;
        Set<String> provinceSet= new HashSet<>();
        query();
        for(nepgResult item:RecordData){
            if(Objects.equals(item.getRealLevel(),"一") || Objects.equals(item.getRealLevel(),"二")){
                aqiGood++;
            }
            provinceSet.add(item.getProvince());
            if(city.contains(item.getCity()) && !city_judge.contains(item.getCity())){
                city_judge.add(item.getCity());
                bigCity++;
            }
        }
        int _province=provinceSet.size();
        proRange= _province *1.0 / 34;
        cityrange=bigCity *1.0/ 105;
        aqiAll_num.setText(String.valueOf(aqiAll));
        aqiGood_num.setText(String.valueOf(aqiGood));
        provinceRange.setText(String.format("%.2f",proRange));
        cityRange.setText(String.format("%.2f",cityrange));
    }
    public void Log_out() throws IOException {
        FXMLUtils.switchToShow(btn_out,"NEPM/view/LoginView.fxml");
    }
    public void back() throws IOException {
        FXMLUtils.switchToShow(btn_out, "NEPM/view/ManagerUIView.fxml");
    }
    public void query(){
        city.add("上海市");
        city.add("北京市");
        city.add("深圳市");
        city.add("重庆市");
        city.add("广州市");
        city.add("成都市");
        city.add("天津市");
        city.add("武汉市");
        city.add("东莞市");
        city.add("西安市");
        city.add("杭州市");
        city.add("佛山市");
        city.add("南京市");
        city.add("沈阳市");
        city.add("青岛市");
        city.add("济南市");
        city.add("长沙市");
        city.add("哈尔滨市");
        city.add("郑州市");
        city.add("昆明市");
        city.add("大连市");
        city.add("南宁市");
        city.add("石家庄市");
        city.add("厦门市");
        city.add("太原市");
        city.add("苏州市");
        city.add("贵阳市");
        city.add("合肥市");
        city.add("乌鲁木齐市");
        city.add("宁波市");
        city.add("无锡市");
        city.add("福州市");
        city.add("长春市");
        city.add("南昌市");
        city.add("常州市");
        city.add("兰州市");
        city.add("中山市");
        city.add("惠州市");
        city.add("汕头市");
        city.add("临沂市");
        city.add("淄博市");
        city.add("温州市");
        city.add("呼和浩特市");
        city.add("绍兴市");
        city.add("唐山市");
        city.add("海口市");
        city.add("柳州市");
        city.add("徐州市");
        city.add("烟台市");
        city.add("洛阳市");
        city.add("邯郸市");
        city.add("珠海市");
        city.add("包头市");
        city.add("保定市");
        city.add("潍坊市");
        city.add("大同市");
        city.add("江门市");
        city.add("赣州市");
        city.add("西宁市");
        city.add("南通市");
        city.add("银川市");
        city.add("扬州市");
        city.add("遵义市");
        city.add("襄阳市");
        city.add("鞍山市");
        city.add("昆山市");
        city.add("莆田市");
        city.add("盐城市");
        city.add("泉州市");
        city.add("咸阳市");
        city.add("台州市");
        city.add("芜湖市");
        city.add("株洲市");
        city.add("淮安市");
        city.add("济宁市");
        city.add("吉林市");
        city.add("大庆市");
        city.add("桂林市");
        city.add("秦皇岛市");
        city.add("湛江市");
        city.add("宜昌市");
        city.add("齐齐哈尔市");
        city.add("抚顺市");
        city.add("上饶市");
        city.add("南充市");
        city.add("义乌市");
        city.add("邢台市");
        city.add("泰安市");
        city.add("开封市");
        city.add("张家口市");
        city.add("新乡市");
        city.add("聊城市");
        city.add("淮南市");
        city.add("十堰市");
        city.add("宜宾市");
        city.add("枣庄市");
        city.add("岳阳市");
        city.add("慈溪市");
        city.add("衡阳市");
        city.add("长治市");
        city.add("连云港市");
        city.add("赤峰市");
        city.add("晋江市");
        city.add("泸州市");
    }
}
